package com.superman.supermarket.client.composite.home.entry;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.user.client.ui.FlowPanel;
import com.superman.mvpframe.client.ajax.PostManAsyncCallBack;
import com.superman.mvpframe.client.gin.XGinjector;
import com.superman.mvpframe.client.utils.XJsonUtil;
import com.superman.supermarket.client.utils.ADConfigUtils;

/**
 * 首页入口模块,小于等于5个时展示一行,大于5个时分两行展示,最多显示10个
 * 
 * @author superman
 * @version 2018年7月20日下午8:48:35
 */
public class HomeEntryListModule extends FlowPanel {

	// 单行最多显示的入口配置
	private final int ENTRY_MAX_COUNT_INLINE = 5;
	private FlowPanel firstPanel;

	private FlowPanel secondPanel;

	/**
	 * 构造方法
	 * 
	 * @author superman
	 * @version 2018年7月20日 下午8:49:09
	 */
	public HomeEntryListModule() {

		firstPanel = new FlowPanel();
		secondPanel = new FlowPanel();

		this.add(firstPanel);
		this.add(secondPanel);

		this.setSize("10rem", "4.266667rem");
		this.getElement().addClassName("flex-v");

		firstPanel.setSize("10rem", "2.133333rem");
		firstPanel.getElement().addClassName("flex-h flex-vc");
		firstPanel.getElement().getStyle().setBackgroundImage("-webkit-linear-gradient(top, rgb(255,255,255), rgb(254,238,234))");

		secondPanel.setSize("10rem", "2.133333rem");
		secondPanel.getElement().addClassName("flex-h flex-vc");
		secondPanel.getElement().getStyle().setBackgroundImage("-webkit-linear-gradient(top, rgb(254,238,234), rgb(250,230,224))");

		getEntryList();
	}

	private void getEntryList() {
		firstPanel.clear();
		secondPanel.clear();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("EN_NAME", ADConfigUtils.getHomeEntryListADENName());
		XGinjector.INSTANCE.getPostMan().post("ad/list", map, new PostManAsyncCallBack() {

			@Override
			public void onBusinessError(String statusCode) {
				HomeEntryListModule.this.removeFromParent();
			}

			@Override
			public void onBusinessOK(String response) {
				int count = XJsonUtil.getCount(response);
				if (count == 0)
					HomeEntryListModule.this.removeFromParent();
				else {
					parse(count, XJsonUtil.getList(response));
				}
			}
		});

	}

	private void parse(int count, List<Map<String, Object>> list) {
		// 小于等于5个入口时,显示一行
		if (count <= ENTRY_MAX_COUNT_INLINE) {
			secondPanel.removeFromParent();
			for (int i = 0; i < list.size(); i++) {
				HomeEntryModule homeEntryModule = new HomeEntryModule();
				homeEntryModule.setData(list.get(i));
				firstPanel.add(homeEntryModule);
			}
		} else {
			// 大于5时,取偶数,分两行展示
			count = count > 10 ? 10 : count;
			count = count % 2 == 1 ? count - 1 : count;
			int scount = count / 2;
			for (int i = 0; i < scount; i++) {
				HomeEntryModule homeEntryModule = new HomeEntryModule();
				homeEntryModule.setData(list.get(i));
				firstPanel.add(homeEntryModule);
			}
			for (int i = scount; i < count; i++) {
				HomeEntryModule homeEntryModule = new HomeEntryModule();
				homeEntryModule.setData(list.get(i));
				secondPanel.add(homeEntryModule);
			}
		}
	}

}
